﻿// ==UserScript==
// @name ISP_DSmanager
// @description A bit help with copying server id and rac
// @author yumaa@ispsystem.com
// @include */manager/dsmgr*
// ==/UserScript==

(function() {
if ( ! /dsmgr/.test(window.location) ) return;

	var tmplt = ujsGetCookie('ujstmplt');
	
	opera.addEventListener('BeforeEvent.DOMContentLoaded', function(e) {
		
		// create prompt button on picture near settings on top
		var topel = $id('rTbl'); 
		if ( topel ) {
			var topelimg = topel.getElementsByClassName('sysbar-img');
			if ( topelimg ) {
				topelimg = topelimg[0];
				topelimg.title = tmplt;
				topelimg.onclick = '';
				topelimg.addEventListener('click', function (e) {
					ujsPrompt('Please enter template string\n(%n - column, :a - all, :n - numbers)', tmplt, 'ujstmplt');
				}, false);
			}
		}
		
		if ( ! ujsCheckPage(Array('title','ds')) ) return;
		if ( tmplt == '' ) return;
		
		var maintbl = $id('MAINTBL');
		if ( !maintbl ) return;
		if ( maintbl.rows[0] )
			var colscnt = maintbl.rows[0].getElementsByTagName('td').length;
		
		var r = tmplt.match(/%\d:[an]/g);
		for (q = 0; q < r.length; q++)
			r[q] = r[q].match(/%(\d):([an])/);
		
		var copystr = '', cnt = '';
		for (i = 0; i < maintbl.rows.length /*mistix*/-3; i++) {
			copystr = tmplt;
			for (q = 0; q < r.length; q++) {
				if (r[q][2] == 'a')  cnt = maintbl.rows[i].cells[r[q][1]-1].innerText;
								else cnt = maintbl.rows[i].cells[r[q][1]-1].innerText.match(/\d+/);
				copystr = copystr.replace(r[q][0], cnt)
			}
			var rowid = maintbl.rows[i].cells[0].innerText;
			maintbl.rows[i].cells[0].innerHTML = '<a href="clip:'+copystr+'">'+rowid+'</a>'; // " onclick="alert('+"'"+copystr+"'"+'); return false;"
		}
		
	}, true);
})();
